<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>date/DateUtil.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ArrayUtil.html">ArrayUtil</a><ul class='methods'><li data-type='method'><a href="ArrayUtil.html#.complement">complement</a></li><li data-type='method'><a href="ArrayUtil.html#.contains">contains</a></li><li data-type='method'><a href="ArrayUtil.html#.defaultIfEmpty">defaultIfEmpty</a></li><li data-type='method'><a href="ArrayUtil.html#.hasNull">hasNull</a></li><li data-type='method'><a href="ArrayUtil.html#.hasUndefined">hasUndefined</a></li><li data-type='method'><a href="ArrayUtil.html#.intersect">intersect</a></li><li data-type='method'><a href="ArrayUtil.html#.isEmpty">isEmpty</a></li><li data-type='method'><a href="ArrayUtil.html#.isNotEmpty">isNotEmpty</a></li><li data-type='method'><a href="ArrayUtil.html#.minus">minus</a></li><li data-type='method'><a href="ArrayUtil.html#.remove">remove</a></li><li data-type='method'><a href="ArrayUtil.html#.removeOne">removeOne</a></li><li data-type='method'><a href="ArrayUtil.html#.unique">unique</a></li><li data-type='method'><a href="ArrayUtil.html#.uniquelize">uniquelize</a></li></ul></li><li><a href="Base64.html">Base64</a><ul class='methods'><li data-type='method'><a href="Base64.html#.decodeStr">decodeStr</a></li><li data-type='method'><a href="Base64.html#.encodeStr">encodeStr</a></li></ul></li><li><a href="BufferUtil.html">BufferUtil</a><ul class='methods'><li data-type='method'><a href="BufferUtil.html#.bufferToStream">bufferToStream</a></li><li data-type='method'><a href="BufferUtil.html#.streamToBuffer">streamToBuffer</a></li></ul></li><li><a href="DatePattern.html">DatePattern</a></li><li><a href="DateUtil.html">DateUtil</a><ul class='methods'><li data-type='method'><a href="DateUtil.html#.dateSecond">dateSecond</a></li><li data-type='method'><a href="DateUtil.html#.dayOfWeek">dayOfWeek</a></li><li data-type='method'><a href="DateUtil.html#.dayOfYear">dayOfYear</a></li><li data-type='method'><a href="DateUtil.html#.format">format</a></li><li data-type='method'><a href="DateUtil.html#.formatChineseDateTime">formatChineseDateTime</a></li><li data-type='method'><a href="DateUtil.html#.formatDate">formatDate</a></li><li data-type='method'><a href="DateUtil.html#.formatDateTime">formatDateTime</a></li><li data-type='method'><a href="DateUtil.html#.formatTime">formatTime</a></li><li data-type='method'><a href="DateUtil.html#.getEndDate">getEndDate</a></li><li data-type='method'><a href="DateUtil.html#.getFirstDayOfMonth">getFirstDayOfMonth</a></li><li data-type='method'><a href="DateUtil.html#.getStartDate">getStartDate</a></li><li data-type='method'><a href="DateUtil.html#.getToDayEndDate">getToDayEndDate</a></li><li data-type='method'><a href="DateUtil.html#.getToDayStartDate">getToDayStartDate</a></li><li data-type='method'><a href="DateUtil.html#.isAM">isAM</a></li><li data-type='method'><a href="DateUtil.html#.isPM">isPM</a></li><li data-type='method'><a href="DateUtil.html#.month">month</a></li><li data-type='method'><a href="DateUtil.html#.nowDate">nowDate</a></li><li data-type='method'><a href="DateUtil.html#.nowDateSecond">nowDateSecond</a></li><li data-type='method'><a href="DateUtil.html#.parse">parse</a></li><li data-type='method'><a href="DateUtil.html#.quarter">quarter</a></li><li data-type='method'><a href="DateUtil.html#.year">year</a></li></ul></li><li><a href="ErrorUtil.html">ErrorUtil</a><ul class='methods'><li data-type='method'><a href="ErrorUtil.html#.getMsg">getMsg</a></li><li data-type='method'><a href="ErrorUtil.html#.getStack">getStack</a></li></ul></li><li><a href="HashUtil.html">HashUtil</a><ul class='methods'><li data-type='method'><a href="HashUtil.html#.fnvHash">fnvHash</a></li><li data-type='method'><a href="HashUtil.html#.hornerHash">hornerHash</a></li></ul></li><li><a href="Ipv4Util.html">Ipv4Util</a><ul class='methods'><li data-type='method'><a href="Ipv4Util.html#.formatIpBlock">formatIpBlock</a></li><li data-type='method'><a href="Ipv4Util.html#.ipv4ToNum">ipv4ToNum</a></li><li data-type='method'><a href="Ipv4Util.html#.isIpv4">isIpv4</a></li><li data-type='method'><a href="Ipv4Util.html#.numToIpv4">numToIpv4</a></li></ul></li><li><a href="MapUtil.html">MapUtil</a><ul class='methods'><li data-type='method'><a href="MapUtil.html#.isEmpty">isEmpty</a></li><li data-type='method'><a href="MapUtil.html#.isNotEmpty">isNotEmpty</a></li><li data-type='method'><a href="MapUtil.html#.mapToObject">mapToObject</a></li><li data-type='method'><a href="MapUtil.html#.objectToMap">objectToMap</a></li></ul></li><li><a href="Md5Util.html">Md5Util</a><ul class='methods'><li data-type='method'><a href="Md5Util.html#.createMd5">createMd5</a></li></ul></li><li><a href="NetUtil.html">NetUtil</a><ul class='methods'><li data-type='method'><a href="NetUtil.html#.getIPV4Address">getIPV4Address</a></li><li data-type='method'><a href="NetUtil.html#.getIPV6Address">getIPV6Address</a></li><li data-type='method'><a href="NetUtil.html#.getMultistageReverseProxyIp">getMultistageReverseProxyIp</a></li><li data-type='method'><a href="NetUtil.html#.isIp">isIp</a></li><li data-type='method'><a href="NetUtil.html#.isIpv4">isIpv4</a></li><li data-type='method'><a href="NetUtil.html#.isIpv6">isIpv6</a></li><li data-type='method'><a href="NetUtil.html#.isUnknown">isUnknown</a></li><li data-type='method'><a href="NetUtil.html#.isUsableLocalPort">isUsableLocalPort</a></li><li data-type='method'><a href="NetUtil.html#.isValidPort">isValidPort</a></li></ul></li><li><a href="ObjectIdUtil.html">ObjectIdUtil</a><ul class='methods'><li data-type='method'><a href="ObjectIdUtil.html#.isValid">isValid</a></li></ul></li><li><a href="ObjectUtil.html">ObjectUtil</a><ul class='methods'><li data-type='method'><a href="ObjectUtil.html#.isEmpty">isEmpty</a></li><li data-type='method'><a href="ObjectUtil.html#.isNotEmpty">isNotEmpty</a></li><li data-type='method'><a href="ObjectUtil.html#.mapToObject">mapToObject</a></li><li data-type='method'><a href="ObjectUtil.html#.objectToMap">objectToMap</a></li></ul></li><li><a href="OSUtil.html">OSUtil</a><ul class='methods'><li data-type='method'><a href="OSUtil.html#.getArch">getArch</a></li><li data-type='method'><a href="OSUtil.html#.getCpus">getCpus</a></li><li data-type='method'><a href="OSUtil.html#.getEOL">getEOL</a></li><li data-type='method'><a href="OSUtil.html#.getFreemem">getFreemem</a></li><li data-type='method'><a href="OSUtil.html#.getHostName">getHostName</a></li><li data-type='method'><a href="OSUtil.html#.getIPV4Address">getIPV4Address</a></li><li data-type='method'><a href="OSUtil.html#.getIPV6Address">getIPV6Address</a></li><li data-type='method'><a href="OSUtil.html#.getPlatform">getPlatform</a></li><li data-type='method'><a href="OSUtil.html#.getTotalmem">getTotalmem</a></li><li data-type='method'><a href="OSUtil.html#.getUptime">getUptime</a></li><li data-type='method'><a href="OSUtil.html#.getUserInfo">getUserInfo</a></li><li data-type='method'><a href="OSUtil.html#.getVersion">getVersion</a></li></ul></li><li><a href="PageUtil.html">PageUtil</a><ul class='methods'><li data-type='method'><a href="PageUtil.html#.rainbow">rainbow</a></li><li data-type='method'><a href="PageUtil.html#.totalPage">totalPage</a></li></ul></li><li><a href="PatternPooL.html">PatternPooL</a></li><li><a href="PhoneUtil.html">PhoneUtil</a><ul class='methods'><li data-type='method'><a href="PhoneUtil.html#.isMobile">isMobile</a></li><li data-type='method'><a href="PhoneUtil.html#.isPhone">isPhone</a></li><li data-type='method'><a href="PhoneUtil.html#.isTel">isTel</a></li></ul></li><li><a href="RandomUtil.html">RandomUtil</a><ul class='methods'><li data-type='method'><a href="RandomUtil.html#.randomEle">randomEle</a></li><li data-type='method'><a href="RandomUtil.html#.randomEnStr">randomEnStr</a></li><li data-type='method'><a href="RandomUtil.html#.randomInt">randomInt</a></li><li data-type='method'><a href="RandomUtil.html#.randomStr">randomStr</a></li></ul></li><li><a href="RandomWeightUtil.html">RandomWeightUtil</a><ul class='methods'><li data-type='method'><a href="RandomWeightUtil.html#add">add</a></li><li data-type='method'><a href="RandomWeightUtil.html#clear">clear</a></li><li data-type='method'><a href="RandomWeightUtil.html#next">next</a></li></ul></li><li><a href="RequestUtil.html">RequestUtil</a><ul class='methods'><li data-type='method'><a href="RequestUtil.html#.getClientIp">getClientIp</a></li><li data-type='method'><a href="RequestUtil.html#.getClientIpByHeaders">getClientIpByHeaders</a></li></ul></li><li><a href="ReUtil.html">ReUtil</a><ul class='methods'><li data-type='method'><a href="ReUtil.html#.test">test</a></li></ul></li><li><a href="Sh1Util.html">Sh1Util</a><ul class='methods'><li data-type='method'><a href="Sh1Util.html#.sh1">sh1</a></li></ul></li><li><a href="Snowflake.html">Snowflake</a><ul class='methods'><li data-type='method'><a href="Snowflake.html#nextId">nextId</a></li></ul></li><li><a href="StreamUtil.html">StreamUtil</a><ul class='methods'><li data-type='method'><a href="StreamUtil.html#.bufferToStream">bufferToStream</a></li></ul></li><li><a href="StringFormatUtil.html">StringFormatUtil</a><ul class='methods'><li data-type='method'><a href="StringFormatUtil.html#.format">format</a></li></ul></li><li><a href="StringUtil.html">StringUtil</a><ul class='methods'><li data-type='method'><a href="StringUtil.html#.hasAllEmpty">hasAllEmpty</a></li><li data-type='method'><a href="StringUtil.html#.hasBlank">hasBlank</a></li><li data-type='method'><a href="StringUtil.html#.hasEmpty">hasEmpty</a></li><li data-type='method'><a href="StringUtil.html#.isAllBlank">isAllBlank</a></li><li data-type='method'><a href="StringUtil.html#.isBlank">isBlank</a></li><li data-type='method'><a href="StringUtil.html#.isEmpty">isEmpty</a></li><li data-type='method'><a href="StringUtil.html#.isNotBlank">isNotBlank</a></li><li data-type='method'><a href="StringUtil.html#.isNotEmpty">isNotEmpty</a></li></ul></li><li><a href="UrlUtil.html">UrlUtil</a><ul class='methods'><li data-type='method'><a href="UrlUtil.html#.queryToObj">queryToObj</a></li></ul></li><li><a href="UUID.html">UUID</a><ul class='methods'><li data-type='method'><a href="UUID.html#.randomUUID">randomUUID</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">date/DateUtil.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>'use strict';
const DatePattern = require('./DatePattern');

/**
 * date工具类
 */
class DateUtil {

    /**
     * 获取当前时间
     * @return {Date} 当前时间对象
     */
    static nowDate() {
        return new Date();
    }

    /**
     * 获取当前秒数
     * @return {number}
     */
    static nowDateSecond() {
        return this.dateSecond(this.nowDate());
    }

    /**
     * 获取日期秒数
     * @param date {Date}
     * @return {number}
     */
    static dateSecond(date) {
        return Math.floor(date.getTime() / 1000);
    }

    /**
     * 获取data的年
     * @param date {Date} 时间
     * @return {number} 4位/年
     */
    static year(date) {
        return date.getFullYear();
    }

    /**
     * 获得指定日期所属季度，从1开始计数 &lt;br/>
     * 注：这里是每个月为一个季度
     * @param date {Date} 时间
     * @return {number} 季度
     */
    static quarter(date) {
        return Math.floor(date.getMonth() / 3+1);
    }

    /**
     * 获取指定日期的月份
     * @param date {Date} 时间
     * @return {number} 第几月
     */
    static month(date) {
        return date.getMonth() + 1;
    }

    /**
     * 获取日期在当年的第几天
     * @param date {Date}
     * @return {number}
     */
    static dayOfYear(date) {
        //一天 = 86400000ms =24 * 60 * 60 * 100

        return Math.ceil((this.nowDate()- new Date(this.year(date).toString())) /86400000 ) + 1;
    }

    /**
     * 获取日期为星期几 0 为周日 1为周一
     * @param date {Date}
     * @return {number}
     */
    static dayOfWeek(date) {
        return date.getDay();
    }

    /**
     * 是否为上午
     * @param date {Date}
     * @return {boolean}
     */
    static isAM(date) {

        return date.getHours() &lt; 12;
    }

    /**
     * 是否为下午
     * @param date {Date}
     * @return {boolean}
     */
    static isPM(date) {
        return !this.isAM(date);
    }


    /**
     * 格式化时间
     * @param date {Date}
     * @param formatStr {string} 例如  yyyy-MM-dd HH:mm:ss
     * @return {string}
     */
    static format(date, formatStr) {

        let o = {
            "M+": this.month(date), //月份
            "d+": date.getDate(), //日
            "H+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": this.quarter(date), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(formatStr)) {
            formatStr = formatStr.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (let k in o) {
            if (new RegExp("(" + k + ")").test(formatStr)) {
                formatStr = formatStr.replace(RegExp.$1, 1 === RegExp.$1.length ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return formatStr;
    }

    /**
     * 格式化时间为 yyyy-MM-dd HH:mm:ss
     * @param date {Date} 指定时间
     * @return {string} 格式化后的日期
     */
    static formatDateTime(date) {
        return this.format(date, DatePattern.NORM_DATETIME_PATTERN);
    }


    /**
     * 格式化时间为 yyyy-MM-dd
     * @param date {Date} 指定时间
     * @return {string} 格式化后的日期
     */
    static formatDate(date) {
        return this.format(date, DatePattern.NORM_DATE_PATTERN);

    }

    /**
     * 格式化时间为 HH:mm:ss
     * @param date {Date} 指定时间
     * @return {string}
     */
    static formatTime(date) {
        return this.format(date, DatePattern.NORM_TIME_PATTERN);
    }


    /**
     *  格式化时间为 中国时间
     *  yyyy年MM月dd日HH时mm分ss秒
     * @param date {Date} 指定时间
     * @return {string} 格式化后的时间字符串
     */
    static formatChineseDateTime(date) {
        return this.format(date, DatePattern.CHINESE_DATE_TIME_PATTERN);
    }

    /**
     * 获取指定日期当日的开始时间
     * @param date  {Date}
     * @return {Date}
     */
    static getStartDate(date) {
        return new Date(date.toLocaleDateString());
    }

    /**
     * 获取当天的开始时间
     * @return {Date}
     */
    static getToDayStartDate() {
        return this.getStartDate(new Date());
    }

    /**
     *  获取某一天的结束时间
     * @param date {Date} 指定时间
     * @return {Date}
     */
    static getEndDate(date) {
        //一天少一毫秒 = 24 * 60 * 60 * 1000 - 1  = 86399999
        return new Date(this.getStartDate(date).getTime()+86399999);
    }

    /**
     * 获取当天的结束时间
     * @return {Date}
     */
    static getToDayEndDate() {
        return this.getEndDate(new Date());
    }

    /**
     * 日期解析
     * &lt;link>https://www.cnblogs.com/liuxianan/p/js-date-format-parse.html&lt;/link>
     * @param dateStr {string}
     * @param format {string} 例如'yyyy-MM-dd'
     * @return {Date|null}
     */
    static parse(dateStr,format){
        format = format || DatePattern.NORM_DATE_PATTERN;
        const obj = {y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0};
        format.replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function(m, $1, $2, $3, $4)
        {
            dateStr = dateStr.replace(new RegExp($1+'(\\d{'+$2.length+'})'+$4), function(_m, _$1)
            {
                obj[$3] = parseInt(_$1);
                return null;
            });
            return null;
        });
        obj.M--; // 月份是从0开始的，所以要减去1
        const date = new Date(obj.y, obj.M, obj.d, obj.H, obj.m, obj.s);
        if(obj.S !== 0){
            date.setMilliseconds(obj.S); // 如果设置了毫秒
        }
        return date;
    }


    /**
     * 获取本月第一天的日期
     * @param date {Date}
     * @return {Date}
     */
    static getFirstDayOfMonth(date){
        date.setDate(1);
        return date;
    }

}

module.exports = DateUtil


</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a> on Thu Jul 01 2021 17:38:06 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
